import { createStore } from 'vuex'

export default createStore({
  state: {
      necessityGoodsInCart: [],
      shopBreadcrumb: []
  },
  mutations: {
      addNecessityGoodToCart(state, good) {
          let index = state.necessityGoodsInCart.findIndex((x) => x.key === good.key || x.key === good);
          if (index !== -1) {
              this.state.necessityGoodsInCart[index].count++;
          } else {
              state.necessityGoodsInCart.push({...good, count: 1});
          }
      },
      removeNecessityGoodFromCart(state, good) {
          let index = state.necessityGoodsInCart.findIndex((x) => x.key === good.key || x.key === good);
          if (index !== -1) {
              if (state.necessityGoodsInCart[index].count === 1) state.necessityGoodsInCart.splice(index, 1);
              else this.state.necessityGoodsInCart[index].count--;
          }
      },
      removeAllNecessityGoodFromCart(state) {
          state.necessityGoodsInCart = [];
      },
      changeShopBreadcrumb(state, paths) {
        state.shopBreadcrumb = paths;
      }
  },
  actions: {
    changeShopBreadcrumb(store, paths) {
      store.commit('changeShopBreadcrumb', paths);
    }
  },
  modules: {
  }
})
